/*package com.example.demoweb.interceptor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.example.demoweb.entity.StreamData;
import com.example.demoweb.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;

@Component
public class RequestInterceptor implements HandlerInterceptor{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		BufferedReader br = request.getReader();
		String inputLine;
	    StringBuffer sb = new StringBuffer();
	     try {
	       while ((inputLine = br.readLine()) != null) {
	    	   sb.append(inputLine);
	       }
	       br.close();
	     } catch (IOException e) {
	       System.out.println("IOException: " + e);
	     }
		InputStream is = request.getInputStream();         
		String contentStr= IOUtils.toString(is, "utf-8");     
		BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
		StringBuffer buffer = new StringBuffer();  
		String line = "";  
		String ss = reader.readLine();
		while ((line = reader.readLine()) != null){  
		     buffer.append(line);  
		}  
		String body = buffer.toString();
		
		Map<String,String[]> dataMap = request.getParameterMap();
		String[] dataArray = dataMap.get("streamData");
		Gson gson = new Gson();
		if(sb.length()>0) {
			@SuppressWarnings("unchecked")
			StreamData<String> streamData = gson.fromJson(sb.toString(),StreamData.class);
			String pwd = encryptStreamData(streamData);
			System.out.println(pwd);
		}
		
		System.out.println(">>>MyInterceptor1>>>>>>>在请求处理之前进行调用（Controller方法调用之前）");
		return true;// 只有返回true才会继续向下执行，返回false取消当前请求
	}

	public static String beanToJSONString(Object bean) {
	    return new Gson().toJson(bean);
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		 System.out.println(">>>MyInterceptor1>>>>>>>请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）");
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println(">>>MyInterceptor1>>>>>>>在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）");
	}
	
	private String encryptStreamData(StreamData<String> data) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		
		Map<String,String> headerMap = getFiledsInfo(data.header);
		Map<String,String> bodyMap = getFiledsInfo(data.body);
		
		List<Entry<String,String>> headerList = new ArrayList<Entry<String,String>>(headerMap.entrySet());
		List<Entry<String,String>> bodyList = new ArrayList<Entry<String,String>>(bodyMap.entrySet());
		
		//list排序
		listSort(headerList);
		listSort(bodyList);
		
		String[] headerArray = new String[headerList.size()];
		for(int i=0;i<headerList.size();i++) {
			headerArray[i] = headerList.get(i).getValue();
		}
		String headerStr = StringUtils.join(headerArray, ",");
		System.out.println(headerStr);
		
		String[] bodyArray = new String[bodyList.size()];
		for(int i=0;i<bodyList.size();i++) {
			bodyArray[i] = bodyList.get(i).getValue();
		}
		String bodyStr = StringUtils.join(bodyArray, ",");
		System.out.println(bodyStr);
		
		StringBuffer sb = new StringBuffer();
		sb.append(headerStr);
		sb.append(bodyStr);
		System.out.println(sb.toString());
		
		MessageDigest md5=MessageDigest.getInstance("MD5");
		byte[] newstr=md5.digest(sb.toString().getBytes("utf-8"));
        //加密后的字符串
		String str = Hex.encodeHexString(newstr);
		System.out.println(str);
		return str;
	}
	*//** 
	 * 根据属性名获取属性值 
	 * *//*  
    private Object getFieldValueByName(String fieldName, Object obj) {  
       try {    
           String firstLetter = fieldName.substring(0, 1).toUpperCase();    
           String getter = "get" + firstLetter + fieldName.substring(1);    
           Method method = obj.getClass().getMethod(getter, new Class[] {});    
           Object value = method.invoke(obj, new Object[] {});    
           return value;    
       } catch (Exception e) {    
           return null;    
       }    
    } 
    
    *//** 
     * 获取属性类型(type)，属性名(name)，属性值(value)的map组成的list 
     * *//*  
    @SuppressWarnings("unchecked")
	private Map<String,String> getFiledsInfo(Object obj){ 
    	Map<String,String> infoMap = new HashMap<String,String>(); 
    	Field[] fields=obj.getClass().getDeclaredFields();
    	if(fields.length>0) {
	         for(int i=0;i<fields.length;i++){  
	        	 if(fields[i].getName() != "this$0") {
	        		 infoMap.put(fields[i].getName(), objToString(getFieldValueByName(fields[i].getName(), obj)));  
	        	 }
	         }  
    	}
     return infoMap;  
    } 
    
    private String objToString(Object obj) {
    	if(obj != null) {
    		return obj.toString();
    	}else {
    		return "";
    	}
    }
    
    private void listSort(List list) {
    	Collections.sort(list, new Comparator<Map.Entry<String, String>>() {  
            public int compare(Map.Entry<String, String> o1,  
                    Map.Entry<String, String> o2) {  
            	return o1.getKey().compareTo(o2.getKey());  
            }  
        });
    }
}
*/